/**
 * @Author: Liu, Yunfeng
 * @DateTime: 2021/3/29 22:35
 * @Description: 1599. 经营摩天轮的最大利润
 */
public class minOperationsMaxProfit {
    public static int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
        if (4 * boardingCost <= runningCost) {
            return -1;
        }
        int count = 0;
        int remain = 0;
        int profit = 0;
        for (int i = 0; i < customers.length; i++) {
            if (customers[i] + remain < 4) {
                if ((customers[i] + remain) * boardingCost <= runningCost) {
                    remain = customers[i] + remain;
                }
                else{
                    remain = 0;
                    count++;
                }
            } else {
                if (remain + customers[i] >= 4) {
                    remain = remain + customers[i] - 4;
                    profit += 4 * boardingCost - runningCost;
                    count++;
                }else{
                    remain = 0;
                    profit += (remain+customers[i])+boardingCost - runningCost;
                    count++;
                }
            }
        }
        if(remain>4){
            profit += (4 * boardingCost - runningCost) * remain / 4;
            count += remain/4;
            remain =remain % 4;
        }
        int remainProfit = remain*boardingCost - runningCost;
        if(remainProfit >= 0){
            profit += remainProfit>0?remainProfit:0;
            count++;
        }
        if (count ==0 ){
            return -1;
        }
        //2+16+22+15+46+10+23+0+13+23+33+14+21+19+47+6+13+28+37+18+7+44+25+11+35+10+12+37+27+16+18+48+41+23+22+14+50+39+14+15+26+9+6+13+24+18+36+50+16+33+13+27+11+37+25+18+21+39+41+1+47+44+39+8+40+40+8+0+5+25+44+24+0+27+15+1
        return count;
    }
    public static void main(String[] args){
        int[] customers = {0,43,37,9,23,35,18,7,45,3,8,24,1,6,37,2,38,15,1,14,39,27,4,25,27,33,43,8,44,30,38,40,20,5,17,27,43,11,6,2,30,49,30,25,32,3,18,23,45,43,30,14,41,17,42,42,44,38,18,26,32,48,37,5,37,21,2,9,48,48,40,45,25,30,49,41,4,48,40,29,23,17,7,5,44,23,43,9,35,26,44,3,26,16,31,11,9,4,28,49,43,39,9,39,37,7,6,7,16,1,30,2,4,43,23,16,39,5,30,23,39,29,31,26,35,15,5,11,45,44,45,43,4,24,40,7,36,10,10,18,6,20,13,11,20,3,32,49,34,41,13,11,3,13,0,13,44,48,43,23,12,23,2};
        int boardingCost=43;
        int runningCost = 54;
        System.out.println(minOperationsMaxProfit(customers,boardingCost,runningCost));
        System.out.println();
        //1745/4=43
    }
}
